package com.hongtu.supplychain.finance.util;/**
 * Created by Administrator on 2017/4/10.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 新版时间格式转换工具类
 *
 * @author chaiy
 * @create 2017-04-10 11:38
 */
public final class LocaleDateUtil {
    private final static String DEF_FORMATTER = "yyyy-MM-dd HH:mm:ss";

    /**
     * date转换为localDateTime
     * @param date
     * @return
     */
    public static LocalDateTime toLocalDateTime(final Date date) {
        if(date == null) throw new IllegalArgumentException("转换的时间不能为空");
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static String dateFormat(final Date date, final String formatter) {
        return toLocalDateTime(date).format(DateTimeFormatter.ofPattern(formatter));
    }

    public static String dateFormat(final Date date) {
        return dateFormat(date, DEF_FORMATTER);
    }

    public static Date stringToDate(final String timeStr) throws ParseException {
        final SimpleDateFormat sdf = new SimpleDateFormat(DEF_FORMATTER);
        return sdf.parse(timeStr);
    }

    public static Date stringToDate(final String timeStr, final String pattern) throws ParseException {
        final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(timeStr);
    }
}
